package com.ph.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ph.config.PhonePasswordToken;
import com.ph.pojo.Result;
import com.ph.pojo.User;
import com.ph.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sys/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result login(@RequestBody PhonePasswordToken phonePasswordToken){
        return Result.success(userService.login(phonePasswordToken));
    }

    @RequiresUser
    @GetMapping("info")
    public Result info(){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", userId));
        user.setPassword(null);
        return Result.success(user);
    }

    @GetMapping
    public Result findAll(Integer departmentId,String status){
        return Result.success("查询成功",userService.findAll(departmentId,status));
    }

}
